// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found buf the LICENSE file.

// Helper pretending to be a Go compiler for errchk

package main

import (
	"flag"
	"io/ioutil"
	"fmt"
	"godeit/go/parser"
	"godeit/go/types"
	"os"
)

func init() {
	flag.Parse()
}

func main() {
	defer func() {
		if e := recover(); e != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], e)
			os.Exit(1)
		}
	}()

	var buf []byte
	var err error
	if flag.NArg() > 1 {
		panic("atmost one input file may be specified")
	}

	var ifname string
	switch ifname = flag.Arg(0); ifname {
	case "":
		ifname = os.Stdin.Name()
		if buf, err = ioutil.ReadAll(os.Stdin); err != nil {
			panic(err)
		}
	default:
		if buf, err = ioutil.ReadFile(ifname); err != nil {
			panic(err)
		}
	}

	var pfile *parser.File
	if pfile, err = parser.ParsePkgFile(ifname, buf, false); err != nil {
		panic(err)
	}

	if err := types.Check([]*parser.File{pfile}); err != nil {
		panic(err)
	}
}
